//
//  MHScrollItemCell.swift
//  Kool Car
//
//  Created by huangweiqiang on 2022/4/23.
//

import UIKit
//typealias MHScrollItemCellBlock = (AnyObject, NSInteger) -> Void
protocol MHScrollItemCellDelegate: NSObjectProtocol {
    ///obj： 选中的对像， hIndex: 水平第几个tableview,；  vIndex: 选中的第几行
    func didSelectMHScrollItemCell(obj: Any, vIndex: NSInteger);
}
class MHScrollItemCell: UICollectionViewCell {
    var delegate: MHScrollItemCellDelegate!
    var tableView: UITableView!
    var dataArray: [Any] = []
    var selectIndex: NSInteger = 9999
    var defaultHighLight: Bool = true
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 168), style: .plain)
        self.tableView.register(MHActionSheetCell.self , forCellReuseIdentifier: "MHActionSheetCell")
        self.tableView.register(SelectSalemanSubCell.self , forCellReuseIdentifier: "SelectSalemanSubCell")
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.backgroundColor = UIColor.clear
        self.tableView.separatorStyle = .none
        self.tableView.separatorStyle = .none
        self.tableView.isScrollEnabled = true
        if #available(iOS 15, *) {
            self.tableView.sectionHeaderTopPadding = 0
        }
        self.tableView.bounces = true
        self.contentView.addSubview(self.tableView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///Fuzhi
    func setMHScrollItemCell(data: [Any], aSelect: Any, hiligLight: Bool? = false) {
        self.dataArray = data
        self.defaultHighLight = hiligLight ?? true
        self.selectIndex = (self.dataArray as NSArray).index(of: aSelect)
        
        self.tableView.reloadData()
        if self.dataArray.count > self.selectIndex {
            self.tableView.scrollToRow(at: IndexPath.init(row: self.selectIndex, section: 0), at: .middle, animated: false)
        }
    }
}

extension MHScrollItemCell: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 56.0
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let tempHeaderView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 0.1))
        
        return tempHeaderView
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let tempFooterView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 0.1))
        tempFooterView.backgroundColor = UIColor.white
        return tempFooterView
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SelectSalemanSubCell", for: indexPath) as! SelectSalemanSubCell
        
        if  indexPath.row == 0 {
            //第一行展示线
            cell.lineView.isHidden = false
        }else{
            cell.lineView.isHidden = true
        }
        
        if self.dataArray.count > indexPath.row {
            let itemModel = self.dataArray[indexPath.row]
            //一般的actionSheet
            cell.lineView.isHidden = false
            if self.selectIndex == indexPath.row && self.defaultHighLight {
                //记录选中状态
                cell.titleLab.textColor = Theme.mainColor
                cell.titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 18)
            }else{
                cell.titleLab.textColor = Theme.titleLabColor
                cell.titleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 16)
            }
            
            if (itemModel as? StoreListModel) != nil {
                cell.titleLab.text = (itemModel as! StoreListModel).name ?? ""
            }else{
                cell.titleLab.text = (itemModel as! String)
            }
        }
        cell.selectionStyle = .default
        cell.clipsToBounds = true
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        self.selectIndex = indexPath.row
        self.tableView.reloadData()
        //返回对像
        if self.dataArray.count > self.selectIndex {
            let itemObj = self.dataArray[indexPath.row] as Any
            self.delegate?.didSelectMHScrollItemCell(obj: itemObj, vIndex: self.selectIndex)
        }
    }
}

